package cn.kgc.springcloud.controller;

import cn.kgc.springcloud.clients.ClazzClient;
import cn.kgc.springcloud.entity.Clazz;
import cn.kgc.springcloud.entity.Group;
import cn.kgc.springcloud.service.GroupService;
import cn.kgc.springcloud.vo.GroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 课工场
 * @date 2024/10/23
 * @description
 */
@RestController
@RequestMapping("groups")
public class GroupController {

    @Resource
    private ClazzClient clazzClient;

    @Resource
    private GroupService groupService;

    @GetMapping
    public List<GroupVO> groupList(){

        List<Group> groups = groupService.list();
        List<GroupVO> collect = groups.stream().map(group -> {
            GroupVO groupVO = new GroupVO();
            BeanUtils.copyProperties(group,groupVO);

            Integer clazzid = group.getClazzid();
            Clazz clazz = clazzClient.getById(clazzid);
            groupVO.setClazz(clazz);
            return groupVO;
        }).collect(Collectors.toList());

        return collect;
    }


    @GetMapping("{id}")
    public Group  getOneGroup(@PathVariable("id") Integer id){

        return groupService.getById(id);
    }

    @PostMapping
    public Group  addGroup(@RequestBody Group group){

        groupService.save(group);

        return  group;
    }



}
